//引入exprexx模块
const express = require("express");
const app = express();
const path = require('path');
const openBrowser = require('react-dev-utils/openBrowser');

let htmlServer;

function initHtmlServer(opt) {
  // const EnumFiles = {
  //   index: path.resolve(__dirname, '../page/index.ejs'),
  //   assets: path.resolve(__dirname, '../page/assets'),
  // }

  const EnumFiles = {
    index: path.resolve(__dirname, '../jsonPage/index.ejs'),
    assets: path.resolve(__dirname, '../jsonPage/assets'),
  }



  app.use(express.static(EnumFiles.assets));


  //参数‘/’可当作设置url的根显示页面，这里即”http://localhost:3000/“访问的页面设置为index.html
  app.get('/', (req, res) => {
    // res.sendFile(EnumFiles.index)        //设置/ 下访问文件位置
    res.render(EnumFiles.index, {
      wsPort: opt.wsPort
    })
  });

  //设置端口3000访问地址，即http://localhost:3000
  htmlServer = app.listen(opt.htmlPort, () => {
    var url = 'http://localhost:' + opt.htmlPort;
    openBrowser(url);
    process.stdout.write('[PageLog]:The logsPrint is running at ' + url);
  });
}

function getHtmlServer() {
 return htmlServer;
}

module.exports = {
  initHtmlServer,
  getHtmlServer
}
